//数据表格
layui.use(['table','form'], function() {
    let table = layui.table,
    form = layui.form;

    form.on('submit(addRole)',function (){
        let roleName = $("input[name='roleName']").val();
        let description = $("input[name='description']").val();
        let roleJson = {"name":roleName,"description":description};
        $.ajax({
            url:"/createNewRole",
            method:"post",
            charset:"UTF-8",
            dataType:"json",
            data:JSON.stringify(roleJson),
            contentType: "application/json;charset=utf-8;",
            success:function (data) {
                if (data == "1"){
                    alert("新增角色成功,请前往权限管理页面分配权限");
                    cleanerText();
                    location.reload();
                    layer.close(layer.index);
                }else {
                    alert("新增角色失败");
                    cleanerText();
                    layer.close(layer.index);
                }
            }
        });
    });


    table.render({
        elem: '#rolesList'
        ,method:"post"
        ,toolbar: '#topTools'
        , url: '/getRoleList'
        // , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        , title: '角色列表'
        ,totalRow: true
        ,parseData:function (res) {
            return {
                "code": res.code==200? 0 : -1,
                "msg": "",
                "count":res.data.count,
                "data":res.data.data
            }
        }
        ,request:{
            pageName: 'curPage',
            limitName:'pageSize'
        }
        , cols: [
            [
                {field: 'id', title: 'ID', width: '10%', fixed: 'left', unresize: true, sort: true,totalRowText: '合计'}
                , {field: 'name', title: '角色名',width:'10%'}
                , {field: 'description', title: '描述',width:'15%'}
                , {field: 'createTime', title: '创建日期',width:'30%',templet:"<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                , {field: 'status', title: '状态',width:'15%',templet:"<div>{{d.status == 1 ?'启用':'禁用'}}</div>"}
                , {fixed: 'right', title: '操作', width:'20%',toolbar: '#roleOperator'}
            ]]
        , page: true
        ,done:function (res,curr,count) {
            // console.log(res,curr,count);
            $('table').css('width', '100%');

    }
});
    //监听行工具事件
    table.on('tool(rolesList)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('确定要删除该角色么', function(index){
                obj.del();
                $.ajax({
                    url:"/delRole",
                    method:"post",
                    data:JSON.stringify(data),
                    dataType:"json",
                    contentType: "application/json;charset=utf-8;",
                    success:function (data) {
                        if (data=="1"){
                            alert("删除成功！");
                            location.reload()
                        }else {
                            alert("删除失败！");
                        }
                    }
                });
                layer.close(index);
            });
        }else if(obj.event === 'enable'){
            layer.confirm("确定启用该角色吗？",function (index) {
                $.ajax({
                    url:"/enableRole",
                    method:"post",
                    data:JSON.stringify(data),
                    dataType:"json",
                    contentType: "application/json;charset=utf-8;",
                    success:function (data) {
                        if (data == "1"){
                            alert("启用成功！");
                            location.reload()
                        }else {
                            alert("启用失败！");
                        }
                    }
                });
                layer.close(index);
            });

        }else if(obj.event === 'disable'){
            layer.confirm("确认禁用该角色吗？",function (index) {
                $.ajax({
                    url:"/disableRole",
                    method:"post",
                    data:JSON.stringify(data),
                    dataType:"json",
                    contentType: "application/json;charset=utf-8;",
                    success:function (data) {
                        if (data == "1"){
                            alert("禁用成功！");
                            location.reload()
                        }else {
                            alert("禁用失败！");
                        }
                    }
                });
                layer.close(index);
            });

        }
    });

});



//创建角色


function cleanerText() {
    $("input[name='roleName']").val("");
    $("input[name='description']").val("");
}


function createRoleModel() {
    layui.use(['layer'], function () {
        let layer = layui.layer, $ = layui.$;
        layer.open({
            type: 1,
            title: '新增角色',
            shadeClose: false,
            area:["60%","60%"],
            content: $('#createRole')
        });
    });
}

